<?php
/**
 * PHPMailer
 *
 */
namespace Org\Yike;
class Mailer {
    //初始化
    public function __construct($config=array()) {
        $this->Host       = $config['host']; // sets the SMTP server
        $this->Port       = $config['port'];                   // set the SMTP port for the GMAIL server
        $this->Username   = $config['auth_username']; // SMTP account username
        $this->Password   = $config['auth_password'];        // SMTP account password
    }

    /**
     * 使用PHPMailer发送邮件
     * @param $title    //邮件标题
     * @param $body //发送内容
     * @param string $smtp_email    //接收邮箱
     * @param string $file  //附件
     * @return bool
     */
    public function send_email($title, $body,$smtp_email='',$file=''){
        Vendor('PHPMailer.PHPMailer');
        $mail = new \PHPMailer(true);

        $mail->IsSMTP();
        $mail->SMTPAuth   = true;                  // enable SMTP authentication
        $mail->Host       = $this->Host; // sets the SMTP server
        $mail->Port       = $this->Port;                   // set the SMTP port for the GMAIL server
        $mail->Username   = $this->Username; // SMTP account username
        $mail->Password   = $this->Password;        // SMTP account password

        $mail->SetFrom($this->Username, '酒店预订信息');	//增加一个发件人地址
        $mail->Subject    = $title;	//这个是标题
        $mail->MsgHTML($body);	//发送内容

        if(is_array($smtp_email)){
            foreach($smtp_email as $k=>$v){
                $mail->AddAddress(trim($v),"YiKeCMS".$k);
            }
        }else{
            $mail->AddAddress($smtp_email, "YiKeCMS");
        }

        if($file){
            $mail->AddAttachment($file);    //发送附件
        }

        if(!$mail->Send()) {
            return $mail->ErrorInfo;
        } else {
            return true;
        }
    }
}
?>